load("@prelude//toolchains:cxx.bzl", "cxx_tools_info_toolchain")
load("@prelude//toolchains:python.bzl", "system_python_bootstrap_toolchain")
load("@prelude//toolchains/cxx/clang:tools.bzl", "path_clang_tools")
load("@prelude//toolchains/msvc:tools.bzl", "find_msvc_tools")
load("@root//buck2_utils/platforms:defs.bzl", "is_remote_enabled")

oncall("build_infra")

find_msvc_tools(
    name = "msvc_tools",
    target_compatible_with = ["config//os:windows"],
    use_path_compilers = is_remote_enabled(),
    use_path_linkers = is_remote_enabled() and not host_info().os.is_windows,
    visibility = ["PUBLIC"],
)

path_clang_tools(
    name = "clang_tools",
    target_compatible_with = ["config//os:linux"],
    visibility = ["PUBLIC"],
)

cxx_tools_info_toolchain(
    name = "cxx",
    #The flags in the below attributes' Windows config are copied from Visual Studio's project "Console App", with some changes listed in each attribute
    c_flags = select({
        "config//os:linux": [],
        #Copied from the C++ flags
        "config//os:windows": [
            "/c",
            "/Z7",
            "/nologo",
            "/W3",
            "/WX-",
            "/diagnostics:column",
            "/sdl",
            "/D_CONSOLE",
            "/D_UNICODE",
            "/DUNICODE",
            "/EHsc",
            "/Zc:forScope",
            "/Zc:inline",
            "/permissive-",
            "/TC",
        ] + select({
            "root//buck2_utils/configuration:debug": [
                "/Od",
                "/D_DEBUG",
                "/RTC1",
                "/MDd",
            ],
            "root//buck2_utils/configuration:release": [
                "/O2",
                "/Oi",
                "/GL",
                "/DNDEBUG",
                "/MD",
                "/Gy",
            ],
        }),
    }),
    cxx_flags = select({
        "config//os:linux": [],
        #Added C++ 20 version flag
        #Removed flags that are already the default: /GS, /fp:precise /Gd /Zc:wchar_t /Gm-
        #Removed deprecated /errorReport:prompt
        #Converted /ZI to /Z7 because I'm guessing /ZI won't play very well with remote execution
        #Removed /FC because it probably doesn't work with remote execution
        #Removed /external:W3 because it's redundant
        #Removed /JMC because it doesn't work with lldb
        "config//os:windows": [
            "/std:c++20",
            "/c",
            "/Z7",
            "/nologo",
            "/W3",
            "/WX-",
            "/diagnostics:column",
            "/sdl",
            "/D_CONSOLE",
            "/D_UNICODE",
            "/DUNICODE",
            "/EHsc",
            "/Zc:forScope",
            "/Zc:inline",
            "/permissive-",
            "/TP",
        ] + select({
            "root//buck2_utils/configuration:debug": [
                "/Od",
                "/D_DEBUG",
                "/RTC1",
                "/MDd",
            ],
            "root//buck2_utils/configuration:release": [
                "/O2",
                "/Oi",
                "/GL",
                "/DNDEBUG",
                "/MD",
                "/Gy",
            ],
        }),
    }),
    cxx_tools_info = select({
        "config//os:linux": ":clang_tools",
        "config//os:windows": ":msvc_tools",
    }),
    link_flags = select({
        "config//os:linux": [],
        #Removed flags that are already the default: /TLBID:1 /DYNAMICBASE /NXCOMPAT /ERRORREPORT:PROMPT /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /SUBSYSTEM:CONSOLE
        #Removed /manifest:embed because I don't know what it does
        #Removed default extra libs that are unnecessary most of the time: kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
        #Added /INCREMENTAL:NO to force non-incremental mode
        "config//os:windows": [
            "/NOLOGO",
            "/DEBUG",
            "/INCREMENTAL:NO",
        ] + select({
            "root//buck2_utils/configuration:debug": [],
            #Converted /LTCG:incremental to /LTCG because I'm guessing incremental actions won't work well with Buck2
            "root//buck2_utils/configuration:release": [
                "/OPT:REF",
                "/OPT:ICF",
                "/LTCG",
            ],
        }),
    }),
    link_style = "static",
    visibility = ["PUBLIC"],
)

system_python_bootstrap_toolchain(
    name = "python_bootstrap",
    visibility = ["PUBLIC"],
)
